package com.example.floristshop;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.floristshop.database.User;
import com.example.floristshop.utils.UserImageUtil;

import org.litepal.LitePal;

import java.util.List;
import java.util.Random;

import de.hdodenhof.circleimageview.CircleImageView;

public class RegisterActivity extends AppCompatActivity {

    private CircleImageView image_civ;
    private EditText name_et, password1_et, password2_et;
    private Button submit_btn, return_btn;

    private String name, password1, password2;
    private int image_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        image_civ = (CircleImageView)findViewById(R.id.register_user_image);
        name_et = (EditText)findViewById(R.id.register_username_input);
        password1_et = (EditText)findViewById(R.id.register_password_input);
        password2_et = (EditText)findViewById(R.id.register_password_input2);
        submit_btn = (Button)findViewById(R.id.register_submit_button);
        return_btn = (Button)findViewById(R.id.register_return_button);

        image_id = 0;
        image_civ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                image_id = (image_id+1)%5;
                image_civ.setImageResource(UserImageUtil.id_to_src(image_id));
            }
        });
        submit_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                name = name_et.getText().toString();
                password1 = password1_et.getText().toString();
                password2 = password2_et.getText().toString();

                if(name.equals("")){
                    Toast.makeText(RegisterActivity.this, "用户名不能为空！", Toast.LENGTH_SHORT).show();
                }else if(password1.equals("")){
                    Toast.makeText(RegisterActivity.this, "密码不能为空！", Toast.LENGTH_SHORT).show();
                }
                else if(nameIsRepeated(name)){
                    Toast.makeText(RegisterActivity.this, "该用户名已存在！", Toast.LENGTH_SHORT).show();
                    name_et.setText("");
                    password1_et.setText("");
                    password2_et.setText("");
                }else if(!(password1.equals(password2))){
                    Toast.makeText(RegisterActivity.this, "两次输入的密码不一致！", Toast.LENGTH_SHORT).show();
                    password1_et.setText("");
                    password2_et.setText("");
                }else{
                    User user = new User();
                    user.setUser_id(generateUserId());
                    user.setName(name);
                    user.setImage_id(image_id);
                    user.setPassword(password1);
                    user.save();
                    Toast.makeText(RegisterActivity.this, "注册成功！", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                    intent.putExtra("name", name);
                    intent.putExtra("password", password1);
                    startActivity(intent);
                    finish();
                }
            }
        });
        return_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private boolean nameIsRepeated(String new_name){
        List<User> users_exist= LitePal.findAll(User.class);
        for(User user_exist: users_exist){
            if(user_exist.getName().equals(new_name)){
                return true;
            }
        }
        return false;
    }

    private long generateUserId(){
        Random rm = new Random();
        long id = (rm.nextLong())%90000+10000;
        List<User> users_exist= LitePal.findAll(User.class);
        User user_exist;
        boolean is_unique = false;

        if(users_exist.size()>0){
            while (!is_unique){
                id = (rm.nextLong())%90000+10000;
                for(int i=0; i<users_exist.size(); i++){
                    user_exist = users_exist.get(i);
                    if(user_exist.getUser_id() == id){
                        is_unique=false;
                        break;
                    }
                    if(i==users_exist.size()-1){
                        is_unique=true;
                    }
                }
            }
        }

        return id;
    }
}